package com.zhoug.fileselector3.selector;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.WindowManager;

import com.zhoug.common3.beans.ValuePair;
import com.zhoug.common3.content.FileType;
import com.zhoug.fileselector3.selector.impl.FileSelectorFactory;
import com.zhoug.zmui3.ZMUIAnimations;
import com.zhoug.zmui3.dialog.list.ZMUIListDialog;
import com.zhoug.zmui3.ints.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

/**
 * 文件选择窗口
 *
 * @Author HK-LJJ
 * @Date 2020/1/10
 * @Description
 */
public class FileSelectorDialog implements IFileSelector {
    private static final int VALUE_IMAGE = 1;
    private static final int VALUE_VIDEO = 2;
    private static final int VALUE_AUDIO = 3;
    private static final int VALUE_WORD = 4;
    private static final int VALUE_EXCEL = 5;
    private static final int VALUE_PDF = 6;
    private static final int VALUE_TXT = 7;
    private ChooseDialog mDialog;
    private List<ValuePair<Integer>> mItems;
    private FileSelector mFileSelector;

    private Activity mActivity;
    private Fragment mFragment;
    private FileSelector.onSelectListener onSelectListener;
    private int flag = FileSelector.FLAG_ALL;

    private @NonNull
    FileSelectedConfig fileSelectedConfig = new FileSelectedConfig();


    private @NonNull
    FileSelector.Config config = new FileSelector.Config();

    public FileSelectorDialog(@NonNull Activity activity) {
        this.mActivity = activity;
    }

    public FileSelectorDialog(@NonNull Fragment fragment) {
        this.mFragment = fragment;
        this.mActivity = fragment.getActivity();
    }

    /**
     * 显示选择Dialog
     */
    public void show() {
        if (mDialog == null) {
            init();
        }

        mDialog.show();
    }

    /**
     * 初始化
     */
    private void init() {
        mItems = initItem();
        mDialog = new ChooseDialog(mActivity);
        mDialog.setData(mItems);
        mDialog.setAttributes(WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
        mDialog.setWindowAnimations(ZMUIAnimations.ANIM_DOWN);
        mDialog.setCanceledOnTouchOutside(true);
        mDialog.setFullWidth(true);
        mDialog.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                mDialog.cancel();
                mFileSelector = createFileChooser();
                //设置配置
                config.maxNum = fileSelectedConfig.maxNum;
                config.singleModel = fileSelectedConfig.singleModel;
                config.maxDuration = fileSelectedConfig.maxDuration;
                config.minDuration = fileSelectedConfig.minDuration;
                config.compressImage = fileSelectedConfig.compressImage;
                config.compressWidth = fileSelectedConfig.compressWidth;
                config.compressHeight = fileSelectedConfig.compressHeight;
                config.compressSize = fileSelectedConfig.compressSize;

                ValuePair<Integer> valuePair = mDialog.getData().get(position);
                switch (valuePair.getValue()) {
                    case VALUE_IMAGE:
                        config.fileType = FileType.IMAGE;
                        config.isCamera = fileSelectedConfig.captureImage;
                        break;
                    case VALUE_VIDEO:
                        config.fileType = FileType.VIDEO;
                        config.isCamera = fileSelectedConfig.captureVideo;

                        break;
                    case VALUE_AUDIO:
                        config.fileType = FileType.AUDIO;
                        config.isCamera = fileSelectedConfig.captureAudio;
                        break;
                    case VALUE_WORD:
                        config.fileType = FileType.WORD;
                        break;
                    case VALUE_EXCEL:
                        config.fileType = FileType.EXCEL;
                        break;
                    case VALUE_PDF:
                        config.fileType = FileType.PDF;
                        break;
                    case VALUE_TXT:
                        config.fileType = FileType.TXT;
                        break;

                }
                mFileSelector.setConfig(config);
                mFileSelector.show();
            }
        });

    }

    /**
     * 创建FileChooser
     *
     * @return
     */
    private FileSelector createFileChooser() {
        if (mFileSelector == null) {
            if (mFragment != null) {
                mFileSelector = FileSelectorFactory.createFileChooser(mFragment);
            } else {
                mFileSelector = FileSelectorFactory.createFileChooser(mActivity);
            }
            mFileSelector.setOnSelectListener(onSelectListener);
        }
        return mFileSelector;
    }

    /**
     * 初始化选项
     *
     * @return
     */
    private List<ValuePair<Integer>> initItem() {
        if (mItems == null) {
            mItems = new ArrayList<>();
        } else {
            mItems.clear();
        }
        if ((flag & FileSelector.FLAG_IMAGE) == FileSelector.FLAG_IMAGE) {
            mItems.add(new ValuePair<>("图片", VALUE_IMAGE));
        }
        if ((flag & FileSelector.FLAG_VIDEO) == FileSelector.FLAG_VIDEO) {
            mItems.add(new ValuePair<>("视频", VALUE_VIDEO));
        }
        if ((flag & FileSelector.FLAG_AUDIO) == FileSelector.FLAG_AUDIO) {
            mItems.add(new ValuePair<>("音频", VALUE_AUDIO));
        }
        if ((flag & FileSelector.FLAG_WORD) == FileSelector.FLAG_WORD) {
            mItems.add(new ValuePair<>("word", VALUE_WORD));
        }
        if ((flag & FileSelector.FLAG_EXCEL) == FileSelector.FLAG_EXCEL) {
            mItems.add(new ValuePair<>("excel", VALUE_EXCEL));
        }
        if ((flag & FileSelector.FLAG_PDF) == FileSelector.FLAG_PDF) {
            mItems.add(new ValuePair<>("pdf", VALUE_PDF));
        }
        if ((flag & FileSelector.FLAG_TXT) == FileSelector.FLAG_TXT) {
            mItems.add(new ValuePair<>("txt", VALUE_TXT));
        }
        return mItems;
    }

    /**
     * 需要在activity或者在Fragment的onActivityResult方法中调用
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Deprecated
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (mFileSelector != null) {
            mFileSelector.onActivityResult(requestCode, resultCode, data);
        }
    }

    /**
     * 支持选择文件的类型
     *
     * @param flag {@link FileSelector#FLAG_IMAGE#FLAG_VIDEO#FLAG_AUDIO#FLAG_ALL}
     */
    public void addFlag(int flag) {
        this.flag |= flag;
    }

    /**
     * @param flag {@link FileSelector#FLAG_IMAGE#FLAG_VIDEO#FLAG_AUDIO#FLAG_ALL}
     * @param flag
     */
    public void setFlag(int flag) {
        this.flag = flag;
    }

    public FileSelector.onSelectListener getOnSelectListener() {
        return onSelectListener;
    }

    /**
     * 文件选择回掉
     *
     * @param onSelectListener
     */
    public void setOnSelectListener(FileSelector.onSelectListener onSelectListener) {
        this.onSelectListener = onSelectListener;
    }

    /**
     * 文件选择配置
     *
     * @return
     */
    @NonNull
    public FileSelectedConfig getFileSelectedConfig() {
        return fileSelectedConfig;
    }

    /**
     * 文件选择配置
     *
     * @param fileSelectedConfig
     */
    public void setFileSelectedConfig(@NonNull FileSelectedConfig fileSelectedConfig) {
        this.fileSelectedConfig = fileSelectedConfig;
    }

    /**
     * 文件选择窗口Dialog
     */
    private static class ChooseDialog extends ZMUIListDialog<ValuePair<Integer>> {

        public ChooseDialog(@NonNull Context context) {
            super(context);
        }

        @Override
        public String getText(ValuePair<Integer> itemData) {
            return itemData.getLabel();
        }

    }


}
